<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>05正则表达式</title>
    <script>
        function checkNum(){
            let num= document.getElementById("num").value;
            let reg=/^[0-9]+$/;
            //判断输入的字符串是否合法
            var res =reg.test(num);
            console.log(res);
        }
        function checkRealName(){
            let realname= document.getElementById("realname").value;
            console.log(realname);
            let reg=/^[\u4e00-\u9fa5]{2,4}$/;
            if(!reg.test(realname)){
                alert("请输入合法2-4位的汉字")
            }
        }
        //有^和$和没有的区别
        function checkLetter(){
            let letter= document.getElementById("letter").value;
            let reg=/ba{1,2}c/;
            let reg2=/^ba{1,2}c$/;
            if(!reg2.test(letter)) alert("该字符串不合法")
        }
    </script>
</head>
<body>
数字：<input type="text" id="num" onblur="checkNum();"><br>
汉字：<input type="text" id="realname" onblur="checkRealName();"><br>
字母：<input type="text" id="letter" onblur="checkLetter();"><br>
</body>
</html>